<template>
  <div class="container">
    <div class="search">
      <el-input placeholder="请输入订单编号查询" style="width: 200px" v-model="ordersNumber"></el-input>
      <el-button type="info" plain style="margin-left: 10px" @click="handleSearch">查询</el-button>
      <el-button type="warning" plain style="margin-left: 10px" @click="handleReset">重置</el-button>
    </div>
    <div class="operation">
      <el-button type="danger" plain style="margin-left: 10px" @click="handleBatchDelete">批量删除</el-button>
    </div>
    <div class="table">
      <el-table :data="tableData" stripe @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column prop="id" label="序号" width="70" align="center" sortable></el-table-column>
        <el-table-column prop="ordersNumber" label="订单编号"></el-table-column>
        <el-table-column prop="goodsName" label="商品名称"></el-table-column>
        <el-table-column prop="goodsPhoto" label="商品图片" width="120">
          <template #default="scope">
            <img :src="scope.row.goodsPhoto" alt="商品图片" style="max-width: 100px; max-height: 100px;">
          </template>
        </el-table-column>      
        <el-table-column prop="username" label="收货人"></el-table-column>
        <el-table-column prop="userPhone" label="手机号"></el-table-column>
        <el-table-column prop="userAddress" label="地址"></el-table-column>
        <el-table-column prop="goodsNumber" label="商品数量"></el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="180"></el-table-column>
        <el-table-column prop="price" label="价格" width="100"></el-table-column>
        <el-table-column label="操作" align="center" width="180">
          <template #default="scope">
            <el-button size="mini" type="danger" plain @click="handleDelete(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
            background
            :current-page="pageNum"
            :page-sizes="[5, 10, 20]"
            :page-size="pageSize"
            layout="total, prev, pager, next"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { findShoppingHistoryService, deleteShoppingHistoryService, orderListService, batchDeleteOrderService } from '@/api/order'

const tableData = ref([])
const pageNum = ref(1)
const pageSize = ref(10)
const total = ref(0)
const ordersNumber = ref('')
const selectedRows = ref([])

const fetchData = async () => {
  try {
    const params = {
      pageNum: pageNum.value,
      pageSize: pageSize.value,
      ordersNumber: ordersNumber.value
    }
    const response = await orderListService(params)
    tableData.value = response.data.list
    total.value = response.data.total
  } catch (error) {
    ElMessage.error('获取购物历史列表失败')
  }
}

const handleDelete = (id) => {
  ElMessageBox.confirm('确定要删除该购物历史吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    try {
      await deleteShoppingHistoryService(id)
      ElMessage.success('删除成功')
      fetchData()
    } catch (error) {
      ElMessage.error('删除失败')
    }
  }).catch(() => {
    ElMessage.info('已取消删除')
  })
}

const handleBatchDelete = () => {
  if (selectedRows.value.length === 0) {
    ElMessage.warning('请选择要删除的购物历史')
    return
  }

  ElMessageBox.confirm('确定要删除选中的购物历史吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    try {
      const ids = selectedRows.value.map(row => row.id)
      await batchDeleteOrderService(ids)
      ElMessage.success('删除成功')
      fetchData()
    } catch (error) {
      ElMessage.error('删除失败')
    }
  }).catch(() => {
    ElMessage.info('已取消删除')
  })
}
const handleSearch = async () => {
  try {
    const response = await findShoppingHistoryService(ordersNumber.value)
    if (response.data) {
      tableData.value = [response.data]
      total.value = 1
    } else {
      ElMessage.warning('未找到该订单编号')
      tableData.value = []
      total.value = 0
    }
  } catch (error) {
    ElMessage.error('查询订单编号失败')
  }
}
const handleReset = () => {
  ordersNumber.value = ''
  pageNum.value = 1
  fetchData()
}


const handleSelectionChange = (val) => {
  selectedRows.value = val
}

const handleSizeChange = (newSize) => {
  pageSize.value = newSize
  fetchData()
}

const handleCurrentChange = (newPage) => {
  pageNum.value = newPage
  fetchData()
}

onMounted(() => {
  fetchData()
})
</script>
<style scoped>
.container {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
  background-color: #fff;
  border-radius: 30px;
}

.search {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
}

.table {
  margin-bottom: 20px;
}

.operation {
  margin-bottom: 20px;
}

.pagination {
  text-align: center;
  margin-top: 20px;
}

.el-button {
  margin: 0 5px;
}
</style>